package com.sugon.controller.basics;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.sugon.annotation.AutoLog;
import com.sugon.dao.ErmsFileDao;
import com.sugon.entity.*;
import com.sugon.service.*;
import com.sugon.utils.PageUtils;
import com.sugon.utils.Query;
import com.sugon.utils.R;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * 附件表Controller
 *
 * @author sugon
 * @date 2019-07-11 10:37:10
 */
@Controller
@RequestMapping("annexfile")
public class AnnexFileController {
    @Autowired
    private AnnexFileService annexFileService;
    @Autowired
    private KnowFileService knowFileService; //知识库附件中间表
    @Autowired
    private DevEquFileService devEquFileService;// 设备技术档案
    @Autowired
    private ToolsTechEquService toolsTechEquService;
    @Autowired
    private ErmsFileService ermsFileService;
    @Autowired
    private ErmsFileDao ermsFileDao;

    /**
     * 用于查看知识库附件列表
     */
    @RequestMapping("/list")
    @RequiresPermissions("annexfile:list")
    @ResponseBody
    public R list(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);

        List<AnnexFileEntity> annexFileList = annexFileService.queryList(query);
        int total = annexFileService.queryTotal(query);

        PageUtils pageUtil = new PageUtils(annexFileList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看ERMS模块资料附件列表
     */
    @RequestMapping("/equList")
    @RequiresPermissions("annexfile:list")
    @ResponseBody
    public R equList(@RequestParam Map<String, Object> params) {
        //查询列表数据
        Query query = new Query(params);
        List<AnnexFileEntity> annexFileList = annexFileService.queryEquList(query);
        int total = annexFileService.queryEquTotal(query);
        PageUtils pageUtil = new PageUtils(annexFileList, total, query.getLimit(), query.getPage());
        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
    @RequiresPermissions("annexfile:info")
    @ResponseBody
    public R info(@PathVariable("id") String id) {
        AnnexFileEntity annexFile = annexFileService.queryObject(id);

        return R.ok().put("annexFile", annexFile);
    }

    /**
     * 保存
     */
    @RequestMapping("/save/{knowId}")
    @RequiresPermissions("annexfile:save")
    @ResponseBody
    public R save(@RequestBody AnnexFileEntity annexFile, @PathVariable("knowId") String knowId) {
        /*
        1、保存附件内容
        2、如果已关联知识，那么同时保存关联关系
         */
        int rows = annexFileService.save(annexFile);
        if (rows > 0) {
            if (StrUtil.isNotEmpty(knowId) && !"null".equals(knowId.trim())) {
                KnowFileEntity knowFile = new KnowFileEntity();
                knowFile.setFileId(annexFile.getId());
                knowFile.setKnowId(knowId);
                knowFileService.save(knowFile);
            }
        }
        return rows > 0 ? R.ok() : R.error();
    }

    /**
     * 保存 - 特种人员档案
     */
    @RequestMapping("/saveUser/{userId}")
    @RequiresPermissions("annexfile:save")
    @ResponseBody
    public R saveUser(@RequestBody AnnexFileEntity annexFile, @PathVariable("userId") String userId) {
        annexFileService.saveUser(annexFile, userId);

        return R.ok();
    }

    /**
     * 保存 - 设备技术档案
     */
    @RequestMapping("/saveDevEqu/{equId}")
    @RequiresPermissions("annexfile:save")
    @ResponseBody
    public R saveDevEqu(@RequestBody AnnexFileEntity annexFile, @PathVariable("equId") String equId) {
        boolean flag = annexFileService.save(annexFile) > 0;
        if (flag) {
            DevEquFileEntity devEquFileEntity = new DevEquFileEntity();
            devEquFileEntity.setFileTypeId(equId);
            devEquFileEntity.setFileId(annexFile.getId());
//            devEquFileEntity.setDevId(annexFile.getDevId());
            devEquFileService.save(devEquFileEntity);
        }
        return flag ? R.ok() : R.error();
    }

    /**
     * 保存 - 设备技术档案
     */
    @RequestMapping("/saveDevEqu")
    @RequiresPermissions("annexfile:save")
    @ResponseBody
    public R saveDevEqu(@RequestBody AnnexFileEntity annexFile) {
        boolean flag = annexFileService.save(annexFile) > 0;
        if (flag) {
            ErmsFileEntity ermsFile = new ErmsFileEntity();
            ermsFile.setFileId(annexFile.getId());
            ermsFile.setModuleId(annexFile.getModuleId());
            ermsFile.setFileTypeId(annexFile.getFileTypeId());
            ermsFile.setModuleType(annexFile.getModuleType());
            ermsFileService.save(ermsFile);
        }
        return flag ? R.ok() : R.error();
    }

    /**
     * 保存--工装档案
     */
    @RequestMapping("/saveToolsEqu/{equId}")
    @RequiresPermissions("annexfile:save")
    @ResponseBody
    public R saveToolsEqu(@RequestBody AnnexFileEntity annexFile, @PathVariable("equId") String equId) {
//        annexFile.setType(AnnexFileModuleType.TOOLS);
//        annexFile.setFileType(equId);
        boolean flag = annexFileService.save(annexFile) > 0;
        if (flag) {
            ToolsTechEquEntity toolsTechEqu = new ToolsTechEquEntity();
            toolsTechEqu.setToolsFileId(equId);
            toolsTechEqu.setFileId(annexFile.getId());
            toolsTechEqu.setType(3);//工装
            toolsTechEquService.save(toolsTechEqu);
        }
        return flag ? R.ok() : R.error();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("annexfile:update")
    @ResponseBody
    public R update(@RequestBody AnnexFileEntity annexFile) {
        annexFileService.update(annexFile);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("annexfile:delete")
    @ResponseBody
    public R delete(@RequestBody String[] ids) {
        /*
            1、此处删除属于逻辑删除
            2、遍历每个待删除的附件ID，判断
         */
        for (String annexfileId : ids) {
            AnnexFileEntity annexFile = annexFileService.queryObject(annexfileId);
            Assert.notNull(annexFile, "附件[{}]不存在，请确认！", annexfileId);
            ermsFileDao.logicDeleteByFileId(annexFile.getId());
        }
        return R.ok();
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    @ResponseBody
    public R queryAll(@RequestParam Map<String, Object> params) {

        List<AnnexFileEntity> list = annexFileService.queryList(params);

        return R.ok().put("list", list);
    }
}
